/*----------------------------------------------------------------------------
Owner(s): Shane Whitfield
$Copyright: $
$Id: $
----------------------------------------------------------------------------*/

#include "wave.h"
#include "go_enemies.h"
#include "timermgr.h"

/* -------------------------------------------------------------------------*/
//globals
static bool (*s_fileLoader)(const char* i_filename, CWave* io_level) = 0 ;

/* -------------------------------------------------------------------------*/

void CWave::SetFileLoader(bool (*systemFileLoader)(const char* i_filename, CWave* io_level))
{
    s_fileLoader = systemFileLoader;
}

CWave::CWave(const char* i_fileName, const Vector2f& i_pos)
{
	m_state = EState_Inactive;
	m_spawnLoc = i_pos;
	
	load(i_fileName);

}

CWave::~CWave()
{
	//delete all the commands
	for (int iCommand = 0; iCommand < m_spawnCommands.size(); ++iCommand)
	{
		delete m_spawnCommands[iCommand];
	}
	
	m_spawnCommands.clear();
}

void CWave::Spawn()
{
	//spawn all the enemies
	for (int iCommand = 0; iCommand < m_spawnCommands.size(); ++iCommand)
	{
		m_spawnCommands[iCommand]->Execute(m_spawnLoc);
		m_enemies.push_back(m_spawnCommands[iCommand]->GetSpawnedGo());
	}
}

void CWave::load(const char* i_fileName)
{
	s_fileLoader(i_fileName, this);
}

bool CWave::IsComplete() const
{
	for (int iGo = 0; iGo < m_enemies.size(); ++iGo)
	{
		if (m_enemies[iGo]->IsAlive())
		{
			return false;
		}
	}
	
	return true;
}

void CWave::On_Spawn(const char* i_classString, float i_x, float i_y)
{
	CSpawnCommand* spawnCommand = new CSpawnCommand(ClassFromString(i_classString), Vector2f(i_x, i_y));
	m_spawnCommands.push_back(spawnCommand);
}

/*--------------------------------------------------------------------------*/ 

void CWave::CSpawnCommand::Execute(const Vector2f& i_waveOrigin)
{
	//spawn the enemy
	m_spawnedGo = static_cast<CGoEnemy*>(g_gomgr.CreateGo(m_spawnClass, m_posOffset + i_waveOrigin));
}

/*-----------------------------------EOF!-----------------------------------*/ 
